package com.aqgj.Resource.web;

import com.aqgj.Resource.bean.Resource;
import com.aqgj.Resource.service.ResourceManager;
import com.aqgj.common.BaseController;
import com.aqgj.shiro.ShiroUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;


@Controller
@RequestMapping("resource")
public class ResourceAction extends BaseController{

    @Autowired
    private ResourceManager resourceManager;

    /**
     * 登录获取菜单树
     *
     * @return
     */
    @RequestMapping("/tree")
    @ResponseBody
    public Object tree() {
        ShiroUser shiroUser = getShiroUser();
        return renderSuccess(resourceManager.selectTree(shiroUser));
    }

    /**
     * 资源管理列表
     *
     * @return
     */
    @PostMapping("/treeGrid")
    @ResponseBody
    public Object treeGrid() {
        return renderSuccess(resourceManager.selectAll());
    }

    /**
     * 添加资源
     *
     * @param resource
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public Object add(Resource resource) {
        resource.setCreateTime(new Date());
        if("".equals(resource.getPid())){
            resource.setPid(null);
        }
        String newId=resourceManager.insertResource(resource);

        if(newId==null){
            return renderError("资源名称："+resource.getUrlName()+"重复或不能为空，请重新填写！");
        }else{
            return renderSuccess("添加成功！");
        }
    }

    /**
     * 查询所有的资源tree
     */
    @RequestMapping("/findRoleResourceListByRoleId")
    @ResponseBody
    public Object findRoleResourceListByRoleId(String id) {
        return renderSuccess(resourceManager.selectResourceIdListByRoleId(id));
    }

    /**
     * 编辑资源
     *
     * @param resource
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public Object edit(Resource resource) {
        resource.setCreateTime(new Date());
        String newId=resourceManager.insertResource(resource);

        if(newId==null){
            return renderError("资源名称："+resource.getUrlName()+"重复或不能为空，请重新填写！");
        }else{
            return renderSuccess("编辑成功！");
        }
    }

    /**
     * 删除资源
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id) {
        resourceManager.deleteById(id);
        return renderSuccess("删除成功！");
    }
}